package com.bot.nft.module.op.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bot.common.exception.CommonException;
import com.bot.common.util.CommonCryptogramUtil;
import com.bot.nft.core.enums.chain.ChainLogEnum;
import com.bot.nft.core.enums.dc.FlowLogEnum;
import com.bot.nft.core.enums.op.AirdropEnum;
import com.bot.nft.module.dc.domain.DcFlowLog;
import com.bot.nft.module.dc.domain.DcNftCert;
import com.bot.nft.module.dc.domain.DcNftInfo;
import com.bot.nft.module.dc.mapper.DcFlowLogMapper;
import com.bot.nft.module.dc.mapper.DcNftInfoMapper;
import com.bot.nft.module.dc.mapper.DcNftCertMapper;
import com.bot.nft.module.dc.service.IDcNftCertService;
import com.bot.nft.module.op.domain.*;
import com.bot.nft.module.op.domain.bo.OpAirInfoBo;
import com.bot.nft.module.op.domain.bo.OpAirListBo;
import com.bot.nft.module.op.domain.vo.*;
import com.bot.nft.module.op.mapper.*;
import com.bot.nft.module.op.service.IOpAirInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

import static com.bot.common.util.CommonCryptogramUtil.doSm4CbcEncrypt;

/**
 * <p>
 * 空投活动服务实现类
 * </p>
 *
 * @author zhangkaige
 * @since 2024-01-24
 */
@Service
public class OpAirInfoServiceImpl implements IOpAirInfoService {


    //空投活动Mapper
    @Resource
    private OpAirInfoMapper airInfoMapper;

    //空投活动持有条件Mapper
    @Resource
    private OpAirHoldLimitMapper airHoldMapper;

    //空投活动白名单Mapper
    @Resource
    private OpAirListMapper airListMapper;

    //空投藏品Mapper
    @Resource
    private OpAirNftMapper airNftMapper;

    //空投记录Mapper
    @Resource
    private OpAirLogMapper airLogMapper;

    //空投执行记录Mapper
    @Resource
    private OpAirExeLogMapper airExeLogMapper;

    //流转记录Mapper
    @Resource
    private DcFlowLogMapper flowLogMapper;

    //藏品证书Mapper
    @Resource
    private DcNftCertMapper certMapper;

    //藏品信息Mapper
    @Resource
    private DcNftInfoMapper nftInfoMapper;

    @Resource
    private IDcNftCertService iDcNftCertService;

    @Override
    public Boolean addAirInfo(OpAirInfoBo airInfoBo) {
        //验证执行时间和空投时间
        Date exeTime = airInfoBo.getExeTime();
        Date snaTime = airInfoBo.getSnaTime();
        if (exeTime.before(snaTime)) {
            throw new CommonException("执行时间不能在快照时间之前");
        }

        //1.空投活动基本信息
        OpAirInfo airInfo = BeanUtil.copyProperties(airInfoBo, OpAirInfo.class);
        int count = airInfoMapper.insert(airInfo);
        boolean flag = count > 0;
        //2.判断活动类型
        Integer conType = airInfo.getConType();
        //3.持有条件限制
        if (conType.equals(AirdropEnum.HOLD_CONDITION.getValue())) {
            //空投活动空投藏品信息为空时抛出异常
            if (CollectionUtils.isEmpty(airInfoBo.getOpAirNftBos())) {
                throw new CommonException("空投艺术品不能为空");
            }
            //空投活动持有条件信息为空时抛出异常
            if (CollectionUtils.isEmpty(airInfoBo.getOpAirHoldLimitBos())) {
                throw new CommonException("持有条件不能为空");
            }

            //空投活动空投藏品信息Bo转Entity
            List<OpAirNft> airNftList = BeanUtil.copyToList(airInfoBo.getOpAirNftBos(), OpAirNft.class);
            for (OpAirNft airNft : airNftList) {
                airNft.setAirId(airInfo.getId());
            }

            //保存空投活动空投藏品信息
            flag = airNftMapper.insertBatch(airNftList);

            //空投活动持有条件项信息Bo转entity
            List<OpAirHoldLimit> airHoldLimitList = BeanUtil.copyToList(airInfoBo.getOpAirHoldLimitBos(),
                    OpAirHoldLimit.class);
            for (OpAirHoldLimit airHoldLimit : airHoldLimitList) {
                airHoldLimit.setAirId(airInfo.getId());
            }
            //报存空投活动持有条件项信息
            boolean airHoldLimitFlag = airHoldMapper.insertBatch(airHoldLimitList);
            flag = flag && airHoldLimitFlag;
        }
        //4.白名单限制类型
        if (conType.equals(AirdropEnum.LIST_CONDITION.getValue())) {
            //空投活动白名单信息为空时抛出异常
            if (CollectionUtils.isEmpty(airInfoBo.getOpAirListBos())) {
                throw new CommonException("空投活动白名单信息不能为空");
            }
            //空投活动白名单信息Bo转entity
            List<OpAirListBo> airListBoList = phoneToMbId(airInfoBo.getOpAirListBos(), airInfo.getId());
            List<OpAirList> airListList = BeanUtil.copyToList(airListBoList, OpAirList.class);

            //空投活动白名单信息
            boolean airListFlag = airListMapper.insertBatch(airListList);
            flag = flag && airListFlag;
        }

        return flag;
    }

    @Override
    public Boolean exeAirdrop(String id) {
        boolean flag = true;
        //验证空投活动状态
        OpAirInfo airInfo = airInfoMapper.selectById(id);
        Integer status = airInfo.getStatus();
        if (status.equals(AirdropEnum.DISABLE_STATUS.getValue())) {
            throw new CommonException("活动未启用");
        }
        if (status.equals(AirdropEnum.ENABLE_STATUS.getValue())) {
            throw new CommonException("活动未快照");
        }
        if (status.equals(AirdropEnum.EXECUTED_STATUS.getValue())) {
            throw new CommonException("活动已执行");
        }
        //验证是否到执行时间
        Date airExeTime = airInfo.getExeTime();
        if (airExeTime.after(new Date())) {
            throw new CommonException("未到执行时间");
        }
        Integer conType = airInfo.getConType();
        //2.查询白名单信息
        LambdaQueryWrapper<OpAirList> listWrapper = new LambdaQueryWrapper<>();
        listWrapper.eq(OpAirList::getAirId, id);
        List<OpAirList> airLists = airListMapper.selectList(listWrapper);
        if (CollectionUtils.isEmpty(airLists)) {
            return true;
        }
        //3.生成藏品证书,流转记录和空投记录
        List<DcNftCert> certList = new ArrayList<>();
        List<DcFlowLog> flowLogList = new ArrayList<>();
        List<OpAirLog> airLogList = new ArrayList<>();
        Map<String, Integer> airNftCountMap = new HashMap<>();

        //3.1持有条件类型空投
        if (conType.equals(AirdropEnum.HOLD_CONDITION.getValue())) {
            //空投藏品信息
            List<OpAirNftVo> airNftList = airNftMapper.selAirNftVoByAirId(id);
            //循环空投藏品
            for (OpAirNftVo airNftVo : airNftList) {
                String classId = airNftVo.getClassId();

                int airNftCount = 0;
                //循环白名单集合
                for (OpAirList airList : airLists) {
                    //空投数量
                    int count = airNftVo.getNum() * airList.getCount();
                    airNftCount = airNftCount + count;
                    for (int i = 0; i < count; i++) {
                        //生成藏品证书
                        DcNftCert nftCert = iDcNftCertService.genCert(airNftVo.getNftId(), airList.getMbId(),
                                ChainLogEnum.BUSINESS_AIRDROP.getValue());
                        certList.add(nftCert);
                        //封装流转记录
                        DcFlowLog dcFlowLog = new DcFlowLog();
                        flowLogList.add(dcFlowLog);
                        dcFlowLog.setId(IdUtil.getSnowflakeNextIdStr());
                        dcFlowLog.setMbId(airList.getMbId());
                        dcFlowLog.setCertId(nftCert.getId());
                        dcFlowLog.setCirculation(FlowLogEnum.AIRDROP.getValue());
                        dcFlowLog.setFlowTime(new Date());
                    }
                    //封装空投记录表
                    OpAirLog airLog = new OpAirLog();
                    airLogList.add(airLog);
                    airLog.setId(IdUtil.getSnowflakeNextIdStr());
                    airLog.setNftId(airNftVo.getNftId());
                    airLog.setMbId(airList.getMbId());
                    airLog.setNum(count);
                    airLog.setAirTime(new Date());
                    airLog.setAirId(id);
                }
                //空投藏品数量Map
                airNftCountMap.put(airNftVo.getNftId(), airNftCount);
            }
        }

        //3.2白名单类型空投
        if (conType.equals(AirdropEnum.LIST_CONDITION.getValue())) {
            for (OpAirList airList : airLists) {
                //执行时间
                Date exeTime = airList.getExeTime();
                //投递状态
                Integer delStatus = airList.getStatus();
                //判断是否投递
                if (delStatus.equals(AirdropEnum.DELIVERY_STATUS.getValue())) {
                    continue;
                }
                //投递到指定投递时间
                if (exeTime != null && exeTime.after(new Date())) {
                    continue;
                }
                //藏品证书标识
                String certId = IdUtil.getSnowflakeNextIdStr();
                //封装藏品证书信息
                DcNftCert dcNftCert = new DcNftCert();
                certList.add(dcNftCert);
                dcNftCert.setId(certId);
                dcNftCert.setNftId(airList.getNftId());
                dcNftCert.setMbId(airList.getMbId());
                dcNftCert.setCode(12);
                dcNftCert.setUnit("博添");
                dcNftCert.setStatus(0);
                dcNftCert.setChainAddr(UUID.randomUUID().toString());
                dcNftCert.setContractAddr("山东省济南市");
                dcNftCert.setReleaseTime(new Date());
                //封装流转记录
                DcFlowLog dcFlowLog = new DcFlowLog();
                flowLogList.add(dcFlowLog);
                dcFlowLog.setId(IdUtil.getSnowflakeNextIdStr());
                dcFlowLog.setMbId(airList.getMbId());
                dcFlowLog.setCertId(certId);
                //流转方式空投
                dcFlowLog.setCirculation(airInfo.getDeliveryType());
                dcFlowLog.setFlowTime(new Date());
                //封装空投记录表
                OpAirLog airLog = new OpAirLog();
                airLogList.add(airLog);
                airLog.setId(IdUtil.getSnowflakeNextIdStr());
                airLog.setNftId(airList.getNftId());
                airLog.setMbId(airList.getMbId());
                airLog.setNum(airList.getCount());
                airLog.setAirTime(new Date());
                airLog.setAirId(id);

            }
        }


        //4.判断库存
        //执行状态
        Integer exeStatus = AirdropEnum.EXECUTION_SUCCESSFUL.getValue();
        //执行描述
        StringBuilder remarkSuc = new StringBuilder();
        StringBuilder remarkFai = new StringBuilder();
        //剩余库存
        Map<String, Integer> newInventoryMap = new HashMap<>();
        List<DcNftInfo> dcNftInfos = nftInfoMapper.selectBatchIds(airNftCountMap.keySet());
        for (DcNftInfo dcNftInfo : dcNftInfos) {
            String nftId = dcNftInfo.getId();
            String name = dcNftInfo.getName();
            Integer inventory = dcNftInfo.getInventory();
            Integer airCount = airNftCountMap.get(nftId);
            //计算剩余库存
            newInventoryMap.put(nftId, inventory - airCount);
            if (inventory < airCount) {
                //活动描述
                remarkFai.append(name).append("的库存为").append(inventory).append("小于")
                        .append("要空投的数量").append(airCount).append("要空投的数量;");
                //活动状态
                exeStatus = AirdropEnum.EXECUTION_FAIL.getValue();
            } else {
                //活动描述
                remarkSuc.append("投递").append(name).append("共计").append(airCount)
                        .append("个,");
            }
        }

        //4.修改库存
        Set<String> nftIdList = newInventoryMap.keySet();
        for (String ntfId : nftIdList) {
            LambdaUpdateWrapper<DcNftInfo> nftWrapper = new LambdaUpdateWrapper<>();
            nftWrapper.eq(DcNftInfo::getId, ntfId).set(DcNftInfo::getInventory, newInventoryMap.get(ntfId));
            nftInfoMapper.update(null, nftWrapper);
        }

        //保存存执行日志
        String remark = Objects.equals(exeStatus, AirdropEnum.EXECUTION_SUCCESSFUL.getValue()) ?
                remarkSuc.toString() : remarkFai.toString();
        OpAirExeLog airExeLog = new OpAirExeLog();
        airExeLog.setId(IdUtil.getSnowflakeNextIdStr());
        airExeLog.setExeTime(new Date());
        airExeLog.setStatus(exeStatus);
        airExeLog.setAirId(id);
        airExeLog.setTaskType(AirdropEnum.DELIVERY_TASK.getValue());
        airExeLog.setRemark(remark.substring(0, remark.length() - 1));
        airExeLogMapper.insert(airExeLog);

        //验证是否有空投
        if (Objects.equals(exeStatus, AirdropEnum.EXECUTION_SUCCESSFUL.getValue())) {
            //藏品证书信息
            certMapper.insertBatch(certList);
            //流转记录信息
            flowLogMapper.insertBatch(flowLogList);
            //空投日志
            airLogMapper.insertBatch(airLogList);
            //修改状态
            LambdaUpdateWrapper<OpAirInfo> airWrapper = new LambdaUpdateWrapper<>();
            airWrapper.eq(OpAirInfo::getId, id).set(OpAirInfo::getStatus, AirdropEnum.EXECUTED_STATUS.getValue());
            int update = airInfoMapper.update(null, airWrapper);

            flag = flag && update > 0;
        }

        return flag;
    }

    @Override
    public Boolean deleteAirdrop(String id) {
        //逻辑删除主表信息
        int count = airInfoMapper.deleteById(id);
        //删除空投藏品
        LambdaQueryWrapper<OpAirNft> airNftWrapper = new LambdaQueryWrapper<>();
        airNftWrapper.eq(OpAirNft::getAirId, id);
        airNftMapper.delete(airNftWrapper);
        //删除空投条件
        LambdaQueryWrapper<OpAirHoldLimit> holdWrapper = new LambdaQueryWrapper<>();
        holdWrapper.eq(OpAirHoldLimit::getAirId, id);
        airHoldMapper.delete(holdWrapper);
        //删除白名单
        LambdaQueryWrapper<OpAirList> listWrapper = new LambdaQueryWrapper<>();
        listWrapper.eq(OpAirList::getAirId, id);
        airListMapper.delete(listWrapper);
        return count > 0;
    }

    @Override
    public Page<OpAirInfoVo> pageAirInfo(Integer pageNo, Integer pageSize, String airName, Integer type,
                                         Integer status) {
        //查询空投信息
        Page<OpAirInfo> airInfoPage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<OpAirInfo> airWrapper = new LambdaQueryWrapper<>();
        airWrapper.eq(OpAirInfo::getConType, type);
        if (StringUtils.hasText(airName)) {
            airWrapper.like(OpAirInfo::getName, airName);
        }
        if (status != null) {
            airWrapper.eq(OpAirInfo::getStatus, status);
        }
        airWrapper.orderByDesc(OpAirInfo::getCreateTime);
        Page<OpAirInfoVo> airInfoVoPage = airInfoMapper.selectVoPage(airInfoPage, airWrapper);

        //查询空投信息子资源
        List<OpAirInfoVo> records = airInfoVoPage.getRecords();
        for (OpAirInfoVo record : records) {
            Integer conType = Integer.valueOf(record.getConType());
            if (conType.equals(AirdropEnum.HOLD_CONDITION.getValue())) {
                String airId = record.getId();
                //查询空投藏品信息
                List<OpAirNftVo> airNftVos = airNftMapper.selAirNftVoByAirId(airId);
                record.setOpAirNftVos(airNftVos);
                //持有限制信息
                List<OpAirHoldLimitVo> airHoldLimitVos = airHoldMapper.selHoldLimitVoByAirId(airId);
                record.setOpAirHoldLimitVos(airHoldLimitVos);
            }
        }
        return airInfoVoPage;
    }

    @Override
    public OpAirInfoVo getAirInfo(String id) {
        //查看空投信息
        OpAirInfoVo airInfoVo = airInfoMapper.selectVoById(id);
        Integer conType = Integer.valueOf(airInfoVo.getConType());
        if (conType.equals(AirdropEnum.HOLD_CONDITION.getValue())) {
            //查询空投藏品信息
            List<OpAirNftVo> airNftVos = airNftMapper.selAirNftVoByAirId(id);
            airInfoVo.setOpAirNftVos(airNftVos);
            //持有限制信息
            List<OpAirHoldLimitVo> airHoldLimitVos = airHoldMapper.selHoldLimitVoByAirId(id);
            airInfoVo.setOpAirHoldLimitVos(airHoldLimitVos);
        }
        if (conType.equals(AirdropEnum.LIST_CONDITION.getValue())) {
            //查询空投白名单信息
            List<OpAirListVo> airListVos = airListMapper.selectVoListByAirId(id);
            for (OpAirListVo airListVo : airListVos) {
                String mbPhone = airListVo.getMbPhone();
                airListVo.setMbPhone(CommonCryptogramUtil.doSm4CbcDecrypt(mbPhone));
            }
            airInfoVo.setOpAirListVos(airListVos);
        }
        return airInfoVo;
    }

    @Override
    public Boolean updateAirInfo(OpAirInfoBo airInfoBo) {
        boolean flag = true;
        //空投活动标识不能为空
        String id = airInfoBo.getId();
        if (!StringUtils.hasText(id)) {
            throw new CommonException("执行时间不能在快照时间之前");
        }
        //验证执行时间和空投时间
        Date exeTime = airInfoBo.getExeTime();
        Date snaTime = airInfoBo.getSnaTime();
        if (exeTime.before(snaTime)) {
            throw new CommonException("执行时间不能在快照时间之前");
        }

        //1.空投活动基本信息
        OpAirInfo airInfo = BeanUtil.copyProperties(airInfoBo, OpAirInfo.class);

        Integer conType = airInfo.getConType();
        //2.持有条件限制修改
        if (conType.equals(AirdropEnum.HOLD_CONDITION.getValue())) {
            //空投活动空投藏品信息为空时抛出异常
            if (CollectionUtils.isEmpty(airInfoBo.getOpAirNftBos())) {
                throw new CommonException("空投艺术品不能为空");
            }
            //空投活动持有条件信息为空时抛出异常
            if (CollectionUtils.isEmpty(airInfoBo.getOpAirHoldLimitBos())) {
                throw new CommonException("持有条件不能为空");
            }

            //删除空投藏品
            LambdaQueryWrapper<OpAirNft> airNftWrapper = new LambdaQueryWrapper<>();
            airNftWrapper.eq(OpAirNft::getAirId, id);
            airNftMapper.delete(airNftWrapper);
            //删除空投条件
            LambdaQueryWrapper<OpAirHoldLimit> holdWrapper = new LambdaQueryWrapper<>();
            holdWrapper.eq(OpAirHoldLimit::getAirId, id);
            airHoldMapper.delete(holdWrapper);

            //空投活动空投藏品信息Bo转Entity
            List<OpAirNft> airNftList = BeanUtil.copyToList(airInfoBo.getOpAirNftBos(), OpAirNft.class);
            for (OpAirNft airNft : airNftList) {
                airNft.setAirId(airInfo.getId());
            }

            //空投活动持有条件项信息Bo转entity
            List<OpAirHoldLimit> airHoldLimitList = BeanUtil.copyToList(airInfoBo.getOpAirHoldLimitBos(),
                    OpAirHoldLimit.class);
            for (OpAirHoldLimit airHoldLimit : airHoldLimitList) {
                airHoldLimit.setAirId(airInfo.getId());
            }

            //保存空投活动空投藏品信息
            flag = airNftMapper.insertBatch(airNftList);

            //保存空投活动持有条件项信息
            boolean airHoldLimitFlag = airHoldMapper.insertBatch(airHoldLimitList);
            flag = flag && airHoldLimitFlag;
        }
        //4.白名单限制类型
        if (conType.equals(AirdropEnum.LIST_CONDITION.getValue())) {
            //空投活动白名单信息为空时抛出异常
            if (CollectionUtils.isEmpty(airInfoBo.getOpAirListBos())) {
                throw new CommonException("空投活动白名单信息不能为空");
            }

            //删除空投白名单
            LambdaQueryWrapper<OpAirList> listWrapper = new LambdaQueryWrapper<>();
            listWrapper.eq(OpAirList::getAirId, id);
            airListMapper.delete(listWrapper);

            //空投活动白名单信息Bo转entity
            List<OpAirListBo> airListBoList = phoneToMbId(airInfoBo.getOpAirListBos(), airInfo.getId());
            List<OpAirList> airListList = BeanUtil.copyToList(airListBoList, OpAirList.class);

            //保存空投活动白名单信息
            boolean airListFlag = airListMapper.insertBatch(airListList);
            flag = flag && airListFlag;
        }
        //修改空投活动基本信息
        int count = airInfoMapper.updateById(airInfo);
        flag = flag && count > 0;
        return flag;
    }

    @Override
    public Boolean enableAirInfo(String id) {
        boolean flag;
        //查询状态
        OpAirInfo airInfo = airInfoMapper.selectById(id);
        //活动类型
        Integer conType = airInfo.getConType();
        //活动类型
        Integer status = airInfo.getStatus();

        //修改状态
        if (conType.equals(AirdropEnum.HOLD_CONDITION.getValue())) {
            if (status.equals(AirdropEnum.DISABLE_STATUS.getValue())) {
                //修改状态
                LambdaUpdateWrapper<OpAirInfo> airInfoWrapper = new LambdaUpdateWrapper<>();
                airInfoWrapper.eq(OpAirInfo::getId, id).set(OpAirInfo::getStatus, AirdropEnum.ENABLE_STATUS.getValue());
                int update = airInfoMapper.update(null, airInfoWrapper);
                flag = update > 0;
            } else {
                throw new CommonException("活动启用中");
            }
        } else {
            if (status.equals(AirdropEnum.DISABLE_STATUS.getValue())) {
                //修改状态
                LambdaUpdateWrapper<OpAirInfo> airInfoWrapper = new LambdaUpdateWrapper<>();
                airInfoWrapper.eq(OpAirInfo::getId, id).set(OpAirInfo::getStatus,
                        AirdropEnum.SNAPSHOT_STATUS.getValue());
                int update = airInfoMapper.update(null, airInfoWrapper);
                flag = update > 0;
            } else {
                throw new CommonException("活动启用中");
            }
        }
        return flag;
    }

    @Override
    public Boolean disableAirInfo(String id) {
        Boolean flag;
        //查询状态
        OpAirInfo airInfo = airInfoMapper.selectById(id);
        //活动类型
        Integer conType = airInfo.getConType();
        //活动类型
        Integer status = airInfo.getStatus();

        //修改状态
        if (conType.equals(AirdropEnum.HOLD_CONDITION.getValue())) {
            if (status.equals(AirdropEnum.ENABLE_STATUS.getValue())) {
                //修改状态
                LambdaUpdateWrapper<OpAirInfo> airInfoWrapper = new LambdaUpdateWrapper<>();
                airInfoWrapper.eq(OpAirInfo::getId, id).set(OpAirInfo::getStatus,
                        AirdropEnum.DISABLE_STATUS.getValue());
                int update = airInfoMapper.update(null, airInfoWrapper);
                flag = update > 0;
            } else {
                throw new CommonException("活动不可禁用");
            }
        } else {
            if (status.equals(AirdropEnum.SNAPSHOT_STATUS.getValue())) {
                //修改状态
                LambdaUpdateWrapper<OpAirInfo> airInfoWrapper = new LambdaUpdateWrapper<>();
                airInfoWrapper.eq(OpAirInfo::getId, id).set(OpAirInfo::getStatus,
                        AirdropEnum.DISABLE_STATUS.getValue());
                int update = airInfoMapper.update(null, airInfoWrapper);
                flag = update > 0;
            } else {
                throw new CommonException("活动不可禁用");
            }
        }
        return flag;
    }

    @Override
    public Page<OpAirLogVo> pageAirLog(Integer pageNo, Integer pageSize, String id, String keyword) {
        Page<OpAirLogVo> airLogVoPage = new Page<>(pageNo, pageSize);
        airLogMapper.pageAirLog(airLogVoPage, id, keyword);
        return airLogVoPage;
    }

    @Override
    public List<OpAirExeLogVo> getAirExeLogList(String id) {
        LambdaQueryWrapper<OpAirExeLog> exeLogWrapper = new LambdaQueryWrapper<>();
        exeLogWrapper.eq(OpAirExeLog::getAirId, id);
        return airExeLogMapper.selectVoList(exeLogWrapper);
    }

    @Override
    public List<OpAirInfo> airInfoList(Integer status, Integer type) {
        LambdaQueryWrapper<OpAirInfo> airInfoWrapper = new LambdaQueryWrapper<>();
        if (status != null) {
            airInfoWrapper.eq(OpAirInfo::getStatus, status);
        }
        if (type != null) {
            airInfoWrapper.eq(OpAirInfo::getConType, type);
        }

        return airInfoMapper.selectList(airInfoWrapper);
    }


    private List<OpAirListBo> phoneToMbId(List<OpAirListBo> airListBoList, String airId) {

        //查询会员id
        List<String> phones = new ArrayList<>();
        for (OpAirListBo opAirListBo : airListBoList) {
            String phone = doSm4CbcEncrypt(opAirListBo.getMbPhone());
            opAirListBo.setMbPhone(phone);
            phones.add(phone);
        }
        List<OpAirListVo> opAirListVos = airListMapper.selectVoListByPhones(phones);


        Map<String, String> mbIdMap = new HashMap<>();
        for (OpAirListVo airListVo : opAirListVos) {
            mbIdMap.put(airListVo.getMbPhone(), airListVo.getMbId());
        }
        for (OpAirListBo airListBo : airListBoList) {
            String mbId = mbIdMap.get(airListBo.getMbPhone());
            if (!StringUtils.hasText(mbId)) {
                throw new CommonException("手机号" + airListBo.getMbPhone() + "不存在");
            }
            airListBo.setMbId(mbId);
            airListBo.setAirId(airId);
            airListBo.setStatus(AirdropEnum.NO_DELIVERY_STATUS.getValue());
        }
        return airListBoList;
    }
}
